import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from head.Init_Driver import SurveyExaminationSystem_Driver


class Register:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.155.1.153:8081/user/register"
        self.driver = SurveyExaminationSystem_Driver.driver
        self.driver.get(self.url)
        # self.driver.maximize_window()

    # 清空输入框
    def ClearInputBox(self):
        self.driver.find_element(By.ID, "name").clear()
        self.driver.find_element(By.ID, "username").clear()
        self.driver.find_element(By.ID, "password").clear()
        self.driver.find_element(By.ID, "rePassword").clear()
        # 刷新页面保证所有输入框清空内容
        self.driver.refresh()

    # 异常注册
    def RegisterFail(self):
        # 填写用户名，密码；不填写账号，确认密码；预期结果：注册失败
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("simon123")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        # 添加显示等待，查看页面指定元素是否存在，不存在报错，存在则继续执行
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#username_help > div")))
        # 断言报错内容文本显示是否符合预期
        assert self.driver.find_element(By.CSS_SELECTOR,"#username_help > div").text == "登录账号不能为空"
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located(
                (By.CSS_SELECTOR,"#rePassword_help > div")))
        assert self.driver.find_element(By.CSS_SELECTOR,"#rePassword_help > div").text == "请再次输入密码"
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 填写用户名，账号，确认密码；不填写密码；预期结果：注册失败
        self.ClearInputBox()
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("simon123")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        # 添加显示等待，查看页面指定元素是否存在，不存在报错，存在则继续执行
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "#password_help > div")))
        # 断言指定元素内容文本是否相等
        assert self.driver.find_element(By.CSS_SELECTOR, "#password_help > div").text == "密码是必填项！"
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 填写账号，密码，确认密码；不填写用户名；预期结果：注册失败
        self.ClearInputBox()
        self.driver.find_element(By.CSS_SELECTOR, "#name").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("simon123")
        self.driver.find_element(By.CSS_SELECTOR, "#rePassword").send_keys("simon123")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        # 添加显示等待，查看页面指定元素是否存在，不存在报错，存在则继续执行
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "#name_help > div")))
        # 断言指定元素内容文本是否相等
        assert self.driver.find_element(By.CSS_SELECTOR, "#name_help > div").text == "用户名是必填项！"
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 都不填写；预期结果：注册失败
        self.ClearInputBox()
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        # 添加显示等待，查看页面指定元素是否存在，不存在报错，存在则继续执行
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "#name_help > div")))
        # 断言指定元素内容文本是否相等
        assert self.driver.find_element(By.CSS_SELECTOR, "#name_help > div").text == "用户名是必填项！"
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR,"#username_help > div")))
        assert self.driver.find_element(By.CSS_SELECTOR,"#username_help > div").text == "登录账号不能为空"
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "#password_help > div")))
        assert self.driver.find_element(By.CSS_SELECTOR, "#password_help > div").text == "密码是必填项！"
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR,"#rePassword_help > div")))
        assert self.driver.find_element(By.CSS_SELECTOR,"#rePassword_help > div").text == "请再次输入密码"
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 填写用户名，密码，确认密码；不填写账号；预期结果：注册失败
        self.ClearInputBox()
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("simon123")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("simon123")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        # 添加显示等待，查看页面指定元素是否存在，不存在报错，存在则继续执行
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR,"#username_help > div")))
        # 断言指定元素内容文本是否相等
        assert self.driver.find_element(By.CSS_SELECTOR,"#username_help > div").text == "登录账号不能为空"
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 填写用户名，账号；不填写密码，确认密码；预期结果：注册失败
        self.ClearInputBox()
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        # 添加显示等待，查看页面指定元素是否存在，不存在报错，存在则继续执行
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, "#password_help > div")))
        # 断言指定元素内容文本是否相等
        assert self.driver.find_element(By.CSS_SELECTOR, "#password_help > div").text == "密码是必填项！"
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR,"#rePassword_help > div")))
        assert self.driver.find_element(By.CSS_SELECTOR,"#rePassword_help > div").text == "请再次输入密码"
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 输入不足长度的密码；预期结果：密码至少6位
        self.ClearInputBox()
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("123")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        # 添加显示等待，查看页面指定元素是否存在，不存在报错，存在则继续执行
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR,"#password_help > div")))
        # 断言指定元素内容文本是否相等
        assert self.driver.find_element(By.CSS_SELECTOR,"#password_help > div").text == "密码至少6位"
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 输入不同的密码和确认密码；预期结果：两次密码不一致
        self.ClearInputBox()
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("simon123")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___1k5Ro > div.main___19HXK > div > form > button").click()
        # 添加显示等待，查看页面指定元素是否存在，不存在报错，存在则继续执行
        WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.CSS_SELECTOR,"#rePassword_help > div")))
        # 断言指定元素内容文本是否相等
        assert self.driver.find_element(By.CSS_SELECTOR,"#rePassword_help > div").text == "两次密码不一致!"
        SurveyExaminationSystem_Driver.Get_ScreenShot()

    def RegisterSucceed(self):
        # 输入格式正确的用户名，账号，密码，确认密码；预期结果：注册成功
        self.ClearInputBox()
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("simon")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("simon123")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("simon123")
        self.driver.find_element(By.XPATH,"//span[text()='注册账户']").click()
        # 添加显示等待并断言
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='账号已存在']")))
        SurveyExaminationSystem_Driver.Get_ScreenShot()

        # 用户名，密码等输入特殊字符，比如空格，斜杠等非英文字符串；预期结果：注册成功
        self.ClearInputBox()
        self.driver.find_element(By.CSS_SELECTOR,"#name").send_keys("wa#$%// \\ng")
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("wa#$%// \\ng")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("wa#$%// \\ng")
        self.driver.find_element(By.CSS_SELECTOR,"#rePassword").send_keys("wa#$%// \\ng")
        self.driver.find_element(By.XPATH,"//span[text()='注册账户']").click()
        # 添加显示等待并断言
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='账号已存在']")))
        SurveyExaminationSystem_Driver.Get_ScreenShot()

# register = Register()
# register.RegisterFail()
# register.RegisterSucceed()
# register.driver.quit()